<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');

/**
 * Class ProjectManagement
 */
class ProjectManagement extends Module
{
	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'mod_project';


	/**
	 * Display a wildcard in the back end
	 * @return string
	 */
	public function generate()
	{
		return parent::generate();
	}


	/**
	 * Generate module
	 */
	protected function compile()
	{
		$offset = 0;
		$limit = null;

		$this->perPage = $this->project_limit;
		$this->news_numberOfItems = 300;

		$this->type_id = $this->Input->get("type_id") ? $this->Input->get("type_id") : $this->Input->post("type_id");
		if ($this->type_id) {
			// Get the total number of items
			$objTotal = $this->Database->execute("SELECT COUNT(*) AS total FROM tl_project WHERE published=1 AND type=".$this->type_id." ORDER BY id");
			$total = $objTotal->total;

			// Split the results
			if ($this->perPage > 0 && (!isset($limit) || $this->news_numberOfItems > $this->perPage))
			{
				// Adjust the overall limit
				if (isset($limit))
				{
					$total = min($limit, $total);
				}

				// Get the current page
				$page = $this->Input->get('page') ? $this->Input->get('page') : 1;

				// Set limit and offset
				$limit = $this->perPage;
				$offset = (max($page, 1) - 1) * $this->perPage;

				// Overall limit
				if ($offset + $limit > $total)
				{
					$limit = $total - $offset;
				}

				// Add the pagination menu
				$objPagination = new Pagination($total, $this->perPage);
				$this->Template->pagination = $objPagination->generate("\n  ");
			}
			$query = "SELECT id,name FROM tl_project_type WHERE id=".$this->type_id;
			$objType = $this->Database->execute($query);
			$arrType = $objType->fetchAllAssoc();

			$arrResult = array();
			$objProject  =$this->Database->prepare("SELECT * FROM tl_project WHERE published=1 AND  type=".$this->type_id." ORDER BY id");
			
			if (isset($limit))
			{
				$objProject->limit($limit, $offset);
			}
			$objProject = $objProject->execute();
			$arrProject = $objProject->fetchAllAssoc();
			$arrTemp = array(
				'type_id' => $arrType[0]['id'], 
				'type' => $arrType[0]['name'], 
				'projects' => $arrProject
			);
			$arrResult[$arrType[0]['id']] = $arrTemp;

			$this->Template->arrProject = $arrResult;
			$this->Template->project_perpage = $this->project_perpage;
			if ($this->type_id) $this->Template->go_back = 1;
		}
		else{
			$query = "SELECT id,name FROM tl_project_type";
			$objType = $this->Database->execute($query);
			$arrType = $objType->fetchAllAssoc();
			$arrResult = array();
			foreach ($arrType as $value) {
				$objProject  =$this->Database->prepare("SELECT * FROM tl_project WHERE published=1 AND type=".$value['id']." ORDER BY id");
				if (isset($this->project_limit))
				{
					$objProject->limit($this->project_limit);
				}
				$objProject = $objProject->execute();
				$arrProject = $objProject->fetchAllAssoc();
				$arrTemp = array(
					'type_id' => $value['id'], 
					'type' => $value['name'], 
					'projects' => $arrProject
				);
				$arrResult[$value['id']] = $arrTemp;
			}
			$this->Template->arrProject = $arrResult;
			$this->Template->project_perpage = $this->project_perpage;
			if ($this->type_id) $this->Template->go_back = 1;
		}
		
	}
}
?>